<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<body>

<script type="text/javascript">
$(function() {
	//alert('ok');
	djt_SS_editIndex = undefined;
	
	var Sobid=<%=request.getAttribute("Sobid")%>;
	//console.log("Sobid"+Sobid);
	
	$('#djt_SS_dg'+Sobid+'').datagrid({
    	url:'../socialSecurity/findSSByPage?id='+Sobid+'',
    	columns:[[
    		{field:'',title:'',checkbox:true},
			{field:'socialSecurityId',title:'编号',width:'50',sortable:true},
			{field:'empName',title:'员工名',width:'100'},
			{field:'socialSecurityTypeId',title:'社保类型',width:'100',
				formatter: function(value,row,index){
					return row.socialSecurityTypeName;
				},
				editor:{
					type:'combobox',
					options:{
					   url:'../socialSecurity/queryTypeAll',
					   valueField:'socialSecurityTypeId',
					   textField:'socialSecurityTypeName',
					   required:true
				}
			}},
			{field:'socialSecurityBaseNumber',title:'社保基数',width:'100',editor:{type:'numberbox',options:{required:true}}},
			{field:'publicReserveFunds',title:'公积金基数',width:'100',editor:{type:'numberbox',options:{required:true}}},
			{field:'socialSecurityCompanyValue',title:'公司应缴总额',width:'100'},
			{field:'socialSecurityPersonValue',title:'个人应缴总额',width:'100'},
			{field:'beginDate',title:'开始时间',width:'115',editor:{type:'datebox',options:{required:true}}},
			{field:'endDate',title:'结束时间',width:'115',editor:{type:'datebox'}},
			{field:'remark',title:'备注',width:'100',editor:{type:'text'}},
			{field:'socialSecuritySobId',title:'所属账套ID',width:'1',hidden:'true'}
		]],
		fit:true,
 	   	fitColumns:false,
 	   	//隔行变色
 	   	striped:true,
  	  	nowrap:true,
		toolbar:'#SStb'+Sobid+'',
		pagination:true,
		pageSize:5,
		pageList:[5,10,20,40],
		border:false,
		sortName:'socialSecurityId',
		sortOrder:'asc',
		onAfterEdit : function(rowIndex,rowData,changes){
			var inserted = $('#djt_SS_dg'+Sobid+'').datagrid('getChanges','inserted');
			var updated = $('#djt_SS_dg'+Sobid+'').datagrid('getChanges','updated');
			var url = '';
			if(updated.length > 0){
				url = '../socialSecurity/updateSS';
			}
			var djt_SS_rowData = JSON.stringify(rowData);
			$.post(url,{djt_SS_rowData},function(e){
				var info = JSON.parse(e);
				//console.log(info);
				if(info.success){
					$('#djt_SS_dg'+Sobid+'').datagrid('acceptChanges');
					$.messager.show({
						title:'成功',
						msg:info.msg,
					});
				}else{
					$('#djt_SS_dg'+Sobid+'').datagrid('rejectChanges');
					$.messager.alert('错误',info.msg,'error');
				}
			})
			djt_SS_editIndex = undefined;
			$('#djt_SS_dg'+Sobid+'').datagrid('unselectAll');
		},
		onDblClickRow : function(rowIndex,rowData){
			if(djt_SS_editIndex != undefined){
				$('#djt_SS_dg'+Sobid+'').datagrid('endEdit',djt_SS_editIndex);
				djt_SS_editIndex = undefined;
			}
			if(djt_SS_editIndex == undefined){
				$('#djt_SS_dg'+Sobid+'').datagrid('beginEdit',rowIndex);
				djt_SS_editIndex = rowIndex;
			}
		}
	});

	//保存添加
	$('#djt_SS_savebtn').bind('click', function(){
		if($('#djt_SS_win_form').form('validate')){
			var djt_SS_rowData = $('#djt_SS_win_form').serialize();
			$.ajax({
				url : '../socialSecurity/insertSS?socialSecuritySobId='+Sobid,
				data : djt_SS_rowData,
				cahce : false,
				dataType : 'json',
				success : function(r){
					$("#djt_SS_win").window('close');
					$.messager.show({
						title:'成功',
						msg:'添加社保信息成功！',
					});
				}
			});
		}
    });
	
	//撤销添加
	$('#djt_SS_reloadbtn').bind('click', function(){
		$("#djt_SS_win_form").form('reset');
    });

});

//保存
function saveSS(){
	var Sobid=<%=request.getAttribute("Sobid")%>;
	$('#djt_SS_dg'+Sobid+'').datagrid('endEdit',djt_SS_editIndex);
}

//删除
function removeSS(){
	var Sobid=<%=request.getAttribute("Sobid")%>;
	var rows = $('#djt_SS_dg'+Sobid+'').datagrid('getSelections');
	if(rows.length > 0){
		$.messager.confirm('请确认','您确定要删除当前所有选中的项目吗？',function(r){
			if(r){
				var ids = [];
				for(var i = 0; i < rows.length; i++){
					ids.push(rows[i].baseSalaryId)
				}
				var djt_SS_rowData = JSON.stringify(ids);
				$.post('../socialSecurity/deleteSS',{djt_SS_rowData},function(e){
					var info = JSON.parse(e);
					if(info.success){
						$('#djt_SS_dg'+Sobid+'').datagrid('load');
						$('#djt_SS_dg'+Sobid+'').datagrid('unselectAll');
						$.messager.show({
							title:'成功',
							msg:info.msg,
						});
					}else{
						$('#djt_SS_dg'+Sobid+'').datagrid('rejectChanges');
						$.messager.alert('错误',info.msg,'error');
					}
				})
				djt_SS_editIndex = undefined;
			}
		})
	}else{
		$.messager.alert('提示','请选择要删除的记录！','error');
	}
}

//编辑
function editSS(){
	var Sobid=<%=request.getAttribute("Sobid")%>;
	var rows = $('#djt_SS_dg'+Sobid+'').datagrid('getSelections');
	if(rows.length == 1){
		if(djt_SS_editIndex != undefined){
			$('#djt_SS_dg'+Sobid+'').datagrid('endEdit',djt_SS_editIndex);
		}
		if(djt_SS_editIndex == undefined){
			var index = $('#djt_SS_dg'+Sobid+'').datagrid('getRowIndex',rows[0]);
			$('#djt_SS_dg'+Sobid+'').datagrid('beginEdit',index);
			djt_SS_editIndex = index;
			$('#djt_SS_dg'+Sobid+'').datagrid('unselectAll');
		}
	}else{
		$.messager.show({
			title:'提示',
			msg:'一次只能编辑一行！',
		});
	}
}

//撤销编辑
function rejectSS(){
	var Sobid=<%=request.getAttribute("Sobid")%>;
	djt_SS_editIndex = undefined;
	$('#djt_SS_dg'+Sobid+'').datagrid('rejectChanges');
	$('#djt_SS_dg'+Sobid+'').datagrid('unselectAll');
} 

//开启添加
function addSS(){
	$('#djt_SS_win').window('open');
}

$("#djt_SS_win").window({
	width:330,
    height:370,
    title:'新增员工社保信息',
    minimizable:false,
    maximizable:false,
    collapsible:false,
    closed:true,
});

$('#djt_SS_from_dep').combobox({
		    url:'../dep/findAllDep',
		    valueField:'depId',
		    textField:'depName',
		    editable:false,
		    onSelect:function(record)                         
		    {
		    	var url ='../socialSecurity/findEmpByDepID?did='+record.depId;
		     	$('#djt_SS_from_emp').combobox('reload',url);
		    }
});

$('#djt_SS_from_emp').combobox({
	valueField:'empId',
    textField:'empName',
    editable:false
});

$('#djt_SS_from_SSType').combobox({
	url:'../socialSecurity/queryTypeAll',
	valueField:'socialSecurityTypeId',
    textField:'socialSecurityTypeName',
});

$('#djt_SS_from_SSBaseNumber').numberbox({
	min:0,
	required:true
});

$('#djt_SS_from_SSFunds').numberbox({
	min:0,
	required:true
});

$('#djt_SS_from_SSbDate').datebox({
    required:true
});

$('#djt_SS_from_SSeDate').datebox({});

$('#djt_SS_from_SSmark').textbox({});

$('#djt_SS_savebtn').linkbutton({
    iconCls: 'icon-save'
});

$('#djt_SS_reloadbtn').linkbutton({
    iconCls: 'icon-reload'
});

</script>

<div class="easyui-layout" fit="true" border="false">
	<div region="center" border="false">
		<table id="djt_SS_dg${Sobid}"></table>
	
		<div id="SStb${Sobid}">
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="addSS()">添加</a>
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="removeSS()">删除</a>
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true" onclick="editSS()">编辑</a>
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" onclick="saveSS()">保存</a>
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="rejectSS()">撤销编辑</a>
		</div>
		
		<!-- 添加的窗口 -->
	<div id="djt_SS_win">	
		<form id="djt_SS_win_form" class="easyui-form" method="post">
			<table>
				<tr>
					<td><label for="depName">部门:</label></td>
					<td><input id="djt_SS_from_dep" name="depName"/></td>
				</tr>
				<tr>
					<td><label for="empName">员工:</label></td>
					<td><input id="djt_SS_from_emp" name="empName" required="required"/></td>
				</tr>
				<tr>
					<td><label for="socialSecurityTypeId">社保类型:</label></td>
					<td><input id="djt_SS_from_SSType" name="socialSecurityTypeId"/></td>
				</tr>
				<tr>
					<td><label for="socialSecurityBaseNumber">社保基数:</label></td>
				<td><input id="djt_SS_from_SSBaseNumber"  name="socialSecurityBaseNumber"/></td>
				</tr>
				<tr>
					<td><label for="publicReserveFunds">公积金基数:</label></td>
					<td><input id="djt_SS_from_SSFunds" name="publicReserveFunds"/></td>
				</tr>
				<tr>
					<td><label for="beginDate">开始时间:</label></td>
					<td><input id="djt_SS_from_SSbDate" name="beginDate"/></td>
				</tr>
				<tr>
					<td><label for="endDate">结束时间:</label></td>
					<td><input id="djt_SS_from_SSeDate" name="endDate"/></td>
				</tr>
				<tr>
					<td><label for="remark">备注:</label></td>
					<td><input id="djt_SS_from_SSmark" name="remark" type="text" class="easyui-textbox"/></td>
				</tr>
				<tr  style="height:20px"></tr>
				<tr>
					<td><a id="djt_SS_savebtn" href="#" >保存</a></td>
					<td><a id="djt_SS_reloadbtn" href="#" >重置</a></td>
				</tr>
			</table>
		</form> 
	</div>
	</div>	
</div>
</body>